/*
 * @(#)PatientIdentity.java
 * Date 2013-08-30
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.model.patient;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

import us.hornerscorners.lamppost.model.common.AbstractPersistableModel;
import us.hornerscorners.lamppost.model.common.InstanceIdentifier;
import us.hornerscorners.lamppost.model.common.PersistableModelType;


/**
 *
 * @author jim
 */
public class PatientIdentity extends AbstractPersistableModel {

    /** Field description */
    private DateTime dateSubsumed;

    /** Field description */
    private InstanceIdentifier identifier;

    /** Field description */
    private final List<PatientIdentityMergeEvent> mergeEvents;

    /** Field description */
    private InstanceIdentifier survivingIdentifier;

    /**
     * Constructs ...
     *
     */
    public PatientIdentity() {

        super(PersistableModelType.PatientIdentity);

        this.mergeEvents = new ArrayList<>();
    }

    /**
     * Constructs ...
     *
     *
     * @param identifier
     */
    public PatientIdentity(InstanceIdentifier identifier) {

        this();
        this.identifier = identifier;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public DateTime getDateSubsumed() {
        return dateSubsumed;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public InstanceIdentifier getIdentifier() {
        return identifier;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public List<PatientIdentityMergeEvent> getMergeEvents() {
        return mergeEvents;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public InstanceIdentifier getSurvivingIdentifier() {
        return survivingIdentifier;
    }

    /**
     * Method description
     *
     *
     * @param patientId
     *
     * @return
     */
    public boolean isNotSameId(InstanceIdentifier patientId) {

        return (isSameId(patientId) == false);
    }

    /**
     * Method description
     *
     *
     * @param patientId
     *
     * @return
     */
    public boolean isSameId(InstanceIdentifier patientId) {

        boolean result = false;

        if (patientId != null) {

            if (this.identifier.isSameId(patientId)) {

                result = true;

            } else {

                // check merge history
                for (PatientIdentityMergeEvent event : this.mergeEvents) {

                    if (event.getSubsumedIdentifier().isSameId(patientId)) {

                        result = true;

                        break;
                    }
                }
            }
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param dateSubsumed
     */
    public void setDateSubsumed(DateTime dateSubsumed) {
        this.dateSubsumed = dateSubsumed;
    }

    /**
     * Method description
     *
     *
     * @param identifier
     */
    public void setIdentifier(InstanceIdentifier identifier) {
        this.identifier = identifier;
    }

    /**
     * Method description
     *
     *
     * @param survivingIdentifier
     */
    public void setSurvivingIdentifier(InstanceIdentifier survivingIdentifier) {
        this.survivingIdentifier = survivingIdentifier;
    }
}
